// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var _css = require("./capitalList.css");
	var router = require("../hash/pageRouter");
	var tools = require("tools");
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		inquireCapitalPageList();

		initEvent();
	};
	var initEvent = function() {
		$("#bankInfoList .blue_search_btn").unbind("click").click(function(){
			loadBlankInfoList();
		});
		
		$("#capitalList .export").unbind("click").click(function(){
			exportCapitalExcel();
		});
	};
	
	var inquireCapitalPageList = function() {
		//搜索条件初始化
		var oData = {
			pageIndex: pageIndex,
			pageSize: pageSize
		};
		tools.loadList("inquireCapitalPageList.json", $("#capitalList .table"), $("#capitalListTpl"), oData, function(){
			initClickEvent();
		});
		
		$("#capitalList .blue_search_btn").unbind("click").click(function(){
			var oData = {
				financeNumber: $("#capitalList input[name=financeNumber]").val(),
				enterpriseName: $("#capitalList input[name=enterpriseName]").val(),
				accountBnakName: $("#capitalList input[name=startDate]").val(),
				accountBnakName: $("#capitalList input[name=endDate]").val(),
				pageIndex: pageIndex,
				pageSize: pageSize
			};
			tools.loadList("inquireCapitalPageList.json", $("#capitalList .table"), $("#capitalListTpl"), oData, function(){
				initClickEvent();
			});
		});
	};
	
	var initClickEvent = function(){
		//企业详情
		$("#capitalList .enterpriseDetail").unbind("click").click(function(){
			router.route("client/clientManage_details.html?enterpriseId=" + $(this).attr("enterpriseId"));
		});
		
		//融资详情
		$("#capitalList .financeDetail").unbind("click").click(function(){
			router.route("financing/financingDetailInfo.html?financeId=" + $(this).attr("financeId"));
		});
		
		//审批详情
		$("#capitalList .approveDetail").unbind("click").click(function(){
			router.route("main/financeApprove.html?financeId=" + $(this).attr("financeId") + "&type=detail");
		});
		
		//融资利息详情
		$("#capitalList .phaseDetail").unbind("click").click(function(){
			router.route("financing/phaseRecordList.html?financeId=" + $(this).attr("financeId"));
		});
		
		//融资利息详情
		$("#capitalList .receivedBail").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=QUARTER_SETTLEMENT&verifiCancelState=true";
		});
		
		//融资放款-已放金额
		$("#capitalList .totalLoanAmount").unbind("click").click(function(){
			location.href = "../financing/loanNoticeList.html?financeId=" + $(this).attr("financeId") + "&type=capital";
		});
		
		//保证金-已收保证金总额
		$("#capitalList .receivedBail").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=FINANCE_AUDIT&verifiCancelState=true";
		});
		
		//保证金-追加保证金总额
		$("#capitalList .receivableAddBail").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=BAIL_ADDITION";
		});
		
		//保证金-追加保证金已收
		$("#capitalList .receivedAddBail").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=BAIL_ADDITION&verifiCancelState=true";
		});
		
		//保证金-已退保证金总额
		$("#capitalList .returnBail").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=BAIL_RETURN&verifiCancelState=true";
		});
		
		//保险费用-保险应收总额
		$("#capitalList .receivableInsureAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=['INSURANCE', 'CLAIM_SETTLEMENT']";
		});
		
		//保险费用-保险已收总额
		$("#capitalList .receivedInsureAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=['INSURANCE', 'CLAIM_SETTLEMENT']&verifiCancelState=true";
		});
		
		//保险费用-保险应付总额
		$("#capitalList .paymentableInsureAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/disbursementManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=['INSURANCE', 'CLAIM_SETTLEMENT']";
		});
		
		//保险费用-保险已付总额
		$("#capitalList .paidInsureAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/disbursementManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=['INSURANCE', 'CLAIM_SETTLEMENT']&verifiCancelState=true";
		});
		
		//其他应收-其他应收总额
		$("#capitalList .receivableOtherAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=['ABNORMAL_EXPENSE']";
		});
		
		//其他应收-其他已收总额
		$("#capitalList .receivedOtherAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=['ABNORMAL_EXPENSE']&verifiCancelState=true";
		});
		
		//其他应付-其他应付总额
		$("#capitalList .paymentableOtherAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/disbursementManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=['ABNORMAL_EXPENSE']";
		});
		
		//其他应付-其他已付总额
		$("#capitalList .paidOtherAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/disbursementManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=['ABNORMAL_EXPENSE']&verifiCancelState=true";
		});
		
		//其他应收-已收融资款总额
		$("#capitalList .totalReceivedAmount").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?financeId=" + $(this).attr("financeId") + "&type=capital&source=RECEIVABLES&verifiCancelState=true";
		});
		 
//		$('#capitalList table').stickySort();
		$('#capitalList table').stickySort({ sortable: true });
	};
	
	//  登陆方法
	var exportCapitalExcel = function() {
		base.fnAjax({
			action: "exportCapitalExcel.json",
			async: false,
			nocode: true,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					window.open("../excelTemplate/资金台账.xls");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	
	return {
		init: init
	};
});